home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / addnod1a / form1.frm (.txt) < prev    next >
Encoding:
Visual Basic Form  |  1998-06-12  |  11.6 KB  |  318 lines

  1. VERSION 5.00
  2. Object = "{6B7E6392-850A-101B-AFC0-4210102A8DA7}#1.2#0"; "comctl32.ocx"
  3. Begin VB.Form Form1 
  4.    Caption         =   "Form1"
  5.    ClientHeight    =   4875
  6.    ClientLeft      =   1785
  7.    ClientTop       =   1740
  8.    ClientWidth     =   5415
  9.    LinkTopic       =   "Form10"
  10.    ScaleHeight     =   4875
  11.    ScaleWidth      =   5415
  12.    Begin ComctlLib.TreeView OrgTree 
  13.       DragIcon        =   "Form1.frx":0000
  14.       Height          =   4335
  15.       Left            =   120
  16.       TabIndex        =   0
  17.       Top             =   480
  18.       Width           =   4575
  19.       _ExtentX        =   8070
  20.       _ExtentY        =   7646
  21.       _Version        =   327682
  22.       LineStyle       =   1
  23.       Style           =   7
  24.       Appearance      =   1
  25.    End
  26.    Begin VB.Image IconImage 
  27.       Height          =   480
  28.       Index           =   3
  29.       Left            =   4800
  30.       Picture         =   "Form1.frx":0152
  31.       Top             =   3480
  32.       Width           =   480
  33.    End
  34.    Begin VB.Image IconImage 
  35.       Height          =   480
  36.       Index           =   2
  37.       Left            =   4800
  38.       Picture         =   "Form1.frx":02A4
  39.       Top             =   3000
  40.       Width           =   480
  41.    End
  42.    Begin VB.Image IconImage 
  43.       Height          =   480
  44.       Index           =   1
  45.       Left            =   4800
  46.       Picture         =   "Form1.frx":03F6
  47.       Top             =   2520
  48.       Width           =   480
  49.    End
  50.    Begin VB.Image TreeImage 
  51.       Height          =   240
  52.       Index           =   6
  53.       Left            =   5160
  54.       Picture         =   "Form1.frx":0548
  55.       Top             =   2160
  56.       Width           =   240
  57.    End
  58.    Begin VB.Image TreeImage 
  59.       Height          =   240
  60.       Index           =   5
  61.       Left            =   5160
  62.       Picture         =   "Form1.frx":064A
  63.       Top             =   1800
  64.       Width           =   240
  65.    End
  66.    Begin VB.Image TreeImage 
  67.       Height          =   240
  68.       Index           =   4
  69.       Left            =   5160
  70.       Picture         =   "Form1.frx":098C
  71.       Top             =   1440
  72.       Width           =   240
  73.    End
  74.    Begin VB.Image TreeImage 
  75.       Height          =   240
  76.       Index           =   3
  77.       Left            =   4800
  78.       Picture         =   "Form1.frx":0CCE
  79.       Top             =   2160
  80.       Width           =   240
  81.    End
  82.    Begin VB.Image TreeImage 
  83.       Height          =   240
  84.       Index           =   2
  85.       Left            =   4800
  86.       Picture         =   "Form1.frx":0DD0
  87.       Top             =   1800
  88.       Width           =   240
  89.    End
  90.    Begin VB.Image TreeImage 
  91.       Height          =   240
  92.       Index           =   1
  93.       Left            =   4800
  94.       Picture         =   "Form1.frx":1112
  95.       Top             =   1440
  96.       Width           =   240
  97.    End
  98.    Begin ComctlLib.ImageList TreeImages 
  99.       Left            =   4800
  100.       Top             =   720
  101.       _ExtentX        =   1005
  102.       _ExtentY        =   1005
  103.       BackColor       =   -2147483643
  104.       MaskColor       =   12632256
  105.       _Version        =   327682
  106.    End
  107.    Begin VB.Menu mnuNodes 
  108.       Caption         =   "&Nodes"
  109.       Begin VB.Menu mnuAddFactory 
  110.          Caption         =   "Add &Factory"
  111.       End
  112.       Begin VB.Menu mnuAddGroup 
  113.          Caption         =   "Add &Group"
  114.       End
  115.       Begin VB.Menu mnuAddPerson 
  116.          Caption         =   "Add &Person"
  117.       End
  118.    End
  119. Attribute VB_Name = "Form1"
  120. Attribute VB_GlobalNameSpace = False
  121. Attribute VB_Creatable = False
  122. Attribute VB_PredeclaredId = True
  123. Attribute VB_Exposed = False
  124. Option Explicit
  125. Private Enum ObjectType
  126.     otNone = 0
  127.     otFactory = 1
  128.     otGroup = 2
  129.     otPerson = 3
  130.     otFactory2 = 4
  131.     otGroup2 = 5
  132.     otPerson2 = 6
  133. End Enum
  134. Private SourceNode As Object
  135. Private SourceType As ObjectType
  136. Private TargetNode As Object
  137. ' ***********************************************
  138. ' Return the node's object type.
  139. ' ***********************************************
  140. Private Function NodeType(test_node As Node) As ObjectType
  141.     If test_node Is Nothing Then
  142.         NodeType = otNone
  143.     Else
  144.         Select Case Left$(test_node.Key, 1)
  145.             Case "f"
  146.                 NodeType = otFactory
  147.             Case "g"
  148.                 NodeType = otGroup
  149.             Case "p"
  150.                 NodeType = otPerson
  151.         End Select
  152.     End If
  153. End Function
  154. ' ***********************************************
  155. ' Prepare the ImageList and TreeView controls.
  156. ' ***********************************************
  157. Private Sub Form_Load()
  158. Dim i As Integer
  159. Dim factory As Node
  160. Dim group As Node
  161. Dim person As Node
  162.     ' Load pictures into the ImageList.
  163.     For i = 1 To 6
  164.         TreeImages.ListImages.Add , , TreeImage(i).Picture
  165.     Next i
  166.     ' Attach the TreeView to the ImageList.
  167.     OrgTree.ImageList = TreeImages
  168.     ' Create some nodes.
  169.     Set factory = OrgTree.Nodes.Add(, , "f R & D", "R & D", otFactory, otFactory2)
  170.     Set group = OrgTree.Nodes.Add(factory, tvwChild, "g Engineering", "Engineering", otGroup, otGroup2)
  171.     Set person = OrgTree.Nodes.Add(group, tvwChild, "p Cameron, Charlie", "Cameron, Charlie", otPerson, otPerson2)
  172.     Set person = OrgTree.Nodes.Add(group, tvwChild, "p Davos, Debbie", "Davos, Debbie", otPerson, otPerson2)
  173.     person.EnsureVisible
  174.     Set group = OrgTree.Nodes.Add(factory, tvwChild, "g Test", "Test", otGroup, otGroup2)
  175.     Set person = OrgTree.Nodes.Add(group, tvwChild, "p Able, Andy", "Andy, Able", otPerson, otPerson2)
  176.     Set person = OrgTree.Nodes.Add(group, tvwChild, "p Baker, Betty", "Baker, Betty", otPerson, otPerson2)
  177.     person.EnsureVisible
  178.     Set factory = OrgTree.Nodes.Add(, , "f Sales & Support", "Sales & Support", otFactory, otFactory2)
  179.     Set group = OrgTree.Nodes.Add(factory, tvwChild, "g Showroom Sales", "Showroom Sales", otGroup, otGroup2)
  180.     Set person = OrgTree.Nodes.Add(group, tvwChild, "p Gaines, Gina", "Gaines, Gina", otPerson, otPerson2)
  181.     person.EnsureVisible
  182.     Set group = OrgTree.Nodes.Add(factory, tvwChild, "g Field Service", "Field Service", otGroup, otGroup2)
  183.     Set person = OrgTree.Nodes.Add(group, tvwChild, "p Helms, Harry", "Helms, Harry", otPerson, otPerson2)
  184.     Set person = OrgTree.Nodes.Add(group, tvwChild, "p Ives, Irma", "Ives, Irma", otPerson, otPerson2)
  185.     Set person = OrgTree.Nodes.Add(group, tvwChild, "p Jackson, Josh", "Jackson, Josh", otPerson, otPerson2)
  186.     person.EnsureVisible
  187.     Set group = OrgTree.Nodes.Add(factory, tvwChild, "g Customer Support", "Customer Support", otGroup, otGroup2)
  188.     Set person = OrgTree.Nodes.Add(group, tvwChild, "p Klug, Karl", "Klug, Karl", otPerson, otPerson2)
  189.     Set person = OrgTree.Nodes.Add(group, tvwChild, "p Landau, Linda", "Landau, Linda", otPerson, otPerson2)
  190.     person.EnsureVisible
  191. End Sub
  192. ' ***********************************************
  193. ' Make the TreeView as large as possible.
  194. ' ***********************************************
  195. Private Sub Form_Resize()
  196.     OrgTree.Move 0, 0, ScaleWidth, ScaleHeight
  197. End Sub
  198. ' Add a new factory.
  199. Private Sub mnuAddFactory_Click()
  200. Dim name As String
  201. Dim factory As Node
  202.     name = InputBox("Factory Name", "New Factory", "")
  203.     If name = "" Then Exit Sub
  204.     Set factory = OrgTree.Nodes.Add(, , "f " & name, name, otFactory, otFactory2)
  205.     factory.EnsureVisible
  206. End Sub
  207. ' Add a new group.
  208. Private Sub mnuAddGroup_Click()
  209. Dim name As String
  210. Dim factory As Node
  211. Dim group As Node
  212.     name = InputBox("Group Name", "New Group", "")
  213.     If name = "" Then Exit Sub
  214.     ' Find the factory that should hold the new group.
  215.     Set factory = OrgTree.SelectedItem
  216.     If NodeType(factory) = otPerson Then _
  217.         Set factory = factory.Parent
  218.     If NodeType(factory) = otGroup Then _
  219.         Set factory = factory.Parent
  220.     Set group = OrgTree.Nodes.Add(factory, tvwChild, "g " & name, name, otGroup, otGroup2)
  221.     group.EnsureVisible
  222. End Sub
  223. Private Sub mnuNodes_Click()
  224. Dim selected_node As Node
  225. Dim selected_type As ObjectType
  226.     Set selected_node = OrgTree.SelectedItem
  227.     If selected_node Is Nothing Then
  228.         selected_type = otNone
  229.     Else
  230.         selected_type = NodeType(selected_node)
  231.     End If
  232.     ' You can always add a factory.
  233.     ' You can add a group if a factory, person, or
  234.     ' group is selected.
  235.     mnuAddGroup.Enabled = (selected_type <> otNone)
  236.     ' You can add a person if a group or person
  237.     ' is selected.
  238.     mnuAddPerson.Enabled = (selected_type = otPerson) _
  239.         Or (selected_type = otGroup)
  240. End Sub
  241. ' Add a new person.
  242. Private Sub mnuAddPerson_Click()
  243. Dim name As String
  244. Dim group As Node
  245. Dim person As Node
  246.     name = InputBox("Person Name", "New Person", "")
  247.     If name = "" Then Exit Sub
  248.     ' Find the group that should hold the new person.
  249.     Set group = OrgTree.SelectedItem
  250.     If NodeType(group) = otPerson Then _
  251.         Set group = group.Parent
  252.     Set person = OrgTree.Nodes.Add(group, tvwChild, "p " & name, name, otPerson, otPerson2)
  253.     person.EnsureVisible
  254. End Sub
  255. ' ***********************************************
  256. ' Save the node pressed so we can drag it later.
  257. ' ***********************************************
  258. Private Sub OrgTree_MouseDown(Button As Integer, Shift As Integer, x As Single, y As Single)
  259.     Set SourceNode = OrgTree.HitTest(x, y)
  260. End Sub
  261. ' ***********************************************
  262. ' Start a drag if one is not in progress.
  263. ' ***********************************************
  264. Private Sub OrgTree_MouseMove(Button As Integer, Shift As Integer, x As Single, y As Single)
  265.     If Button = vbLeftButton Then
  266.         ' Start a new drag. Note that we do not get
  267.         ' other MouseMove events while the drag is
  268.         ' in progress.
  269.         
  270.         ' See what node we are dragging.
  271.         SourceType = NodeType(SourceNode)
  272.         ' Select this node. When no node is highlighted,
  273.         ' this node will be displayed as selected. That
  274.         ' shows where it will land if dropped.
  275.         Set OrgTree.SelectedItem = SourceNode
  276.         ' Set the drag icon for this source.
  277.         OrgTree.DragIcon = IconImage(SourceType)
  278.         OrgTree.Drag vbBeginDrag
  279.     End If
  280. End Sub
  281. ' ***********************************************
  282. ' The user is dropping. See if the drop is valid.
  283. ' ***********************************************
  284. Private Sub OrgTree_DragDrop(Source As Control, x As Single, y As Single)
  285.     If Not (OrgTree.DropHighlight Is Nothing) Then
  286.         ' It's a valid drop. Set source node's
  287.         ' parent to be the target node.
  288.         Set SourceNode.Parent = OrgTree.DropHighlight
  289.         Set OrgTree.DropHighlight = Nothing
  290.     End If
  291.     Set SourceNode = Nothing
  292.     SourceType = otNone
  293. End Sub
  294. ' ***********************************************
  295. ' The mouse is being dragged over the control.
  296. ' Highlight the appropriate node.
  297. ' ***********************************************
  298. Private Sub OrgTree_DragOver(Source As Control, x As Single, y As Single, State As Integer)
  299. Dim target As Node
  300. Dim highlight As Boolean
  301.     ' See what node we're above.
  302.     Set target = OrgTree.HitTest(x, y)
  303.     ' If it's the same as last time, do nothing.
  304.     If target Is TargetNode Then Exit Sub
  305.     Set TargetNode = target
  306.     highlight = False
  307.     If Not (TargetNode Is Nothing) Then
  308.         ' See what kind of node were above.
  309.         If NodeType(TargetNode) + 1 = SourceType Then _
  310.             highlight = True
  311.     End If
  312.     If highlight Then
  313.         Set OrgTree.DropHighlight = TargetNode
  314.     Else
  315.         Set OrgTree.DropHighlight = Nothing
  316.     End If
  317. End Sub
  318.